package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanIcpCarArticle.LOAN_ICP_CAR_ARTICLE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanIcpComment.LOAN_ICP_COMMENT;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.family.IcpWebCommentRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.IcpWebCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebCommentSearchResponse;
import com.tuanzi.server.loan_base.util.DateUtils;

/**
 * ICP备案网站评论 JOOQRepository 实现
 * @author JinRuiyang 2017/12/13 14:09:02
 */
@Repository
public class IcpWebCommentRepositoryImpl extends JOOQBaseRepository implements IcpWebCommentRepository {
    /**
     * ICPWebComment查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<IcpWebCommentSearchResponse> list(IcpWebCommentSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_ICP_COMMENT.fields());
        fields(LOAN_ICP_CAR_ARTICLE.TITLE);
        innerJoin(LOAN_ICP_CAR_ARTICLE, LOAN_ICP_COMMENT.PRODUCT_ID.eq(LOAN_ICP_CAR_ARTICLE.ID));
        contains(LOAN_ICP_CAR_ARTICLE.TITLE,searchRequest.getProductName());
        contains(LOAN_ICP_COMMENT.USER_NAME,searchRequest.getUserName());
        ge(LOAN_ICP_COMMENT.CREATE_TIME,searchRequest.getStartTime());
        le(LOAN_ICP_COMMENT.CREATE_TIME,searchRequest.getEndTime());
        orderBy(LOAN_ICP_COMMENT.CREATE_TIME, Sort.Direction.DESC);
        return search(pageable, record -> {
            IcpWebCommentSearchResponse response = new IcpWebCommentSearchResponse();
            response.setId(record.get(LOAN_ICP_COMMENT.ID));
            response.setPk(record.get(LOAN_ICP_COMMENT.ID));
            response.setContent(record.get(LOAN_ICP_COMMENT.CONTENT));
            response.setCreateTime(DateUtils.formatDateToYyyyMMddHHmmss(record.get(LOAN_ICP_COMMENT.CREATE_TIME)));
            response.setUserName(record.get(LOAN_ICP_COMMENT.USER_NAME));
            response.setProductName(record.get(LOAN_ICP_CAR_ARTICLE.TITLE));
            response.setStatus(record.get(LOAN_ICP_COMMENT.STATUS,Integer.class));
            response.setIsEnable(record.get(LOAN_ICP_COMMENT.IS_ENABLE,Integer.class));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_ICP_COMMENT;
    }
}
